package com.integral.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.integral.biz.IBannerImgBiz;
import com.integral.entity.BannerImg;
import com.integral.entity.UserInfo;
import com.integral.util.UploadImgUtil;
import com.integral.vo.BankTypeVo;
import com.integral.vo.BannerImgVo;

@Controller
@RequestMapping("/bannerImg")
public class BannerImgController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private IBannerImgBiz bannerImgBiz;
	
	/**
	 * 查询轮播图列表
	 * @param bannerImgVo
	 * @return
	 */
	@RequestMapping(value="/findBannerImg")
	@ResponseBody
	public Map<String,Object> findBannerImg(BannerImgVo bannerImgVo){
		List<BannerImgVo> list = bannerImgBiz.findBannerImg(bannerImgVo);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", list.size());
		map.put("rows", list);
		return map;
	}
	/**
	 * 新增轮播图
	 * @param request
	 * @param bannerImgVo
	 * @return
	 */
	@RequestMapping(value="/addBannerImg")
	public String addBannerImg(HttpServletRequest request,BannerImgVo bannerImgVo){
		BannerImg bannerImg = new BannerImg();
		//上传文件
		String fileName = UploadImgUtil.dowloadFile(bannerImgVo.getImg(), request);
		if("false".equals(fileName)){
			bannerImg.setBannerImg("");
		}else{
			bannerImg.setBannerImg(fileName);
		}
		//首页Banner图类型
		bannerImg.setBannerType(1);
		bannerImg.setDeleteFlag(0);
		bannerImg.setCreateEmp("SYS");
		bannerImg.setCreateTime(new Date());
		bannerImgBiz.addBannerImg(bannerImg);
		return "management/system";
	}
	/**
	 * 修改轮播图
	 * @param bannerImgVo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateBannerImg")
	@ResponseBody
	public String updateBannerImg(BannerImgVo bannerImgVo,HttpServletRequest request){
		String msg = null;
		try {
			//获取登录对象
			HttpSession session = request.getSession();
			UserInfo user = (UserInfo)session.getAttribute("userInfo");
			bannerImgVo.setUpdateEmp(user.getUserName());
			bannerImgVo.setUpdateTime(new Date());
			//图片
			if(null != bannerImgVo.getImg()){
				String Img = UploadImgUtil.dowloadFile(bannerImgVo.getImg(), request);
				if("false".equals(Img)){
					bannerImgVo.setBannerImg(null);
				}else{
					bannerImgVo.setBannerImg(Img);
				}
			}
			bannerImgBiz.updateBannerImg(bannerImgVo);
			msg = "修改成功！";
		} catch (Exception e) {
			msg = "修改失败！";
			logger.error("修改轮播图异常",e);
		}
		return msg;
	}
	
	/**
	 * 删除轮播图
	 * @param bannerImgVo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delBannerImg")
	@ResponseBody
	public String delBannerImg(BannerImgVo bannerImgVo,HttpServletRequest request){
		String msg = null;
		try {
			bannerImgBiz.delBannerImg(bannerImgVo);
			msg = "删除成功！";
		} catch (Exception e) {
			msg = "删除失败！";
			logger.error("删除轮播图异常",e);
		}
		return msg;
	}
	
}
